1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Data.SqlClient;
10 using System.Security.Cryptography;
11 namespace WarehouseManagementSystem
12 {
13 public partial class frmCustomers : Form
14 {
15
16 SqlConnection con = null;
17 SqlCommand cmd = null;
18 ConnectionString cs = new ConnectionString();
19 public frmCustomers()
20 {
21 InitializeComponent();
22 }
23 private void Reset()
24 {
25 txtAddress.Text = "";
26 txtCity.Text = "";
27 txtEmail.Text = "";
28 txtCustomerName.Text = "";
29 txtContactNo1.Text = "";
30 txtNotes.Text = "";
31 txtContactNo.Text = "";
32 txtCustomerID.Text = "";
33 btnSave.Enabled = true;
34 btnDelete.Enabled = false;
35 btnUpdate.Enabled = false;
36 txtCustomerName.Focus();
37
38 }
39 private void frmCustomers_Load(object sender, EventArgs e)
40 {
41
42 }
43 private void auto()
44 {
45 txtCustomerID.Text = "C-" + GetUniqueKey(6);
46 }
47 public static string GetUniqueKey(int maxSize)
48 {
49 char[] chars = new char[62];
50 chars = "123456789".ToCharArray();
51 byte[] data = new byte[1];
52 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
53 crypto.GetNonZeroBytes(data);
54 data = new byte[maxSize];
55 crypto.GetNonZeroBytes(data);
56 StringBuilder result = new StringBuilder(maxSize);
57 foreach (byte b in data)
58 {
59 result.Append(chars[b % (chars.Length)]);
60 }
61 return result.ToString();
62 }
63
64
65 private void btnNew_Click(object sender, EventArgs e)
66 {
67 Reset();
68 }
69
70 private void btnSave_Click(object sender, EventArgs e)
71 {
72 if (txtCustomerName.Text == "")
73 {
74 MessageBox.Show("Please enter name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
75 txtCustomerName.Focus();
76 return;
77 }
78
79 if (txtAddress.Text == "")
80 {
81 MessageBox.Show("Please enter address", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
82 txtAddress.Focus();
83 return;
84 }
85 if (txtCity.Text == "")
86 {
87 MessageBox.Show("Please enter city", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
88 txtCity.Focus();
89 return;
90 }
91
92 if (txtContactNo.Text == "")
93 {
94 MessageBox.Show("Please enter contact no.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
95 txtContactNo.Focus();
96 return;
97 }
98
99 try
100 {
101 auto();
102
103 con = new SqlConnection(cs.DBConn);
104 con.Open();
105
106 string cb = "insert into Customer(CustomerID,Customername,address,City,ContactNo,ContactNo1,Email,Notes) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8)";
107
108 cmd = new SqlCommand(cb);
109
110 cmd.Connection = con;
111 cmd.Parameters.AddWithValue("@d1", txtCustomerID.Text);
112 cmd.Parameters.AddWithValue("@d2", txtCustomerName.Text);
113 cmd.Parameters.AddWithValue("@d3", txtAddress.Text);
114 cmd.Parameters.AddWithValue("@d4", txtCity.Text);
115 cmd.Parameters.AddWithValue("@d5", txtContactNo.Text);
116 cmd.Parameters.AddWithValue("@d6", txtContactNo1.Text);
117 cmd.Parameters.AddWithValue("@d7", txtEmail.Text);
118 cmd.Parameters.AddWithValue("@d8", txtNotes.Text);
119
120 cmd.ExecuteReader();
121 MessageBox.Show("Successfully saved", "Customer Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
122 btnSave.Enabled = false;
123 if (con.State == ConnectionState.Open)
124 {
125 con.Close();
126 }
127
128 con.Close();
129
130 }
131 catch (Exception ex)
132 {
133 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
134 }
135 }
136 private void delete_records()
137 {
138
139 try
140 {
141
142 int RowsAffected = 0;
143 con = new SqlConnection(cs.DBConn);
144 con.Open();
145 string cq = "delete from Customer where CustomerID='" + txtCustomerID.Text + "'";
146 cmd = new SqlCommand(cq);
147 cmd.Connection = con;
148
149 RowsAffected = cmd.ExecuteNonQuery();
150
151 if (RowsAffected > 0)
152 {
153 MessageBox.Show("Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
154 Reset();
155 }
156 else
157 {
158 MessageBox.Show("No record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information);
159 Reset();
160 }
161 con.Close();
162
163 }
164 catch (Exception ex)
165 {
166 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
167 }
168 }
169
170 private void btnDelete_Click(object sender, EventArgs e)
171 {
172 try
173 {
174
175
176 if (MessageBox.Show("Do you really want to delete the record?", "Customer Record", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
177 {
178 delete_records();
179 }
180
181 }
182 catch (Exception ex)
183 {
184 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
185 }
186 }
187
188 private void btnUpdate_Click(object sender, EventArgs e)
189 {
190 try
191 {
192 if (txtCustomerName.Text == "")
193 {
194 MessageBox.Show("Please enter name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
195 txtCustomerName.Focus();
196 return;
197 }
198
199 if (txtAddress.Text == "")
200 {
201 MessageBox.Show("Please enter address", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
202 txtAddress.Focus();
203 return;
204 }
205 if (txtCity.Text == "")
206 {
207 MessageBox.Show("Please enter city", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
208 txtCity.Focus();
209 return;
210 }
211
212 if (txtContactNo.Text == "")
213 {
214 MessageBox.Show("Please enter contact no.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
215 txtContactNo.Focus();
216 return;
217 }
218 con = new SqlConnection(cs.DBConn);
219 con.Open();
220 string cb = "update Customer set Customername=@d2,address=@d3,City=@d4,ContactNo=@d5,ContactNo1=@d6,Email=@d7,Notes=@d8 where CustomerID=@d1";
221
222 cmd = new SqlCommand(cb);
223
224 cmd.Connection = con;
225 cmd.Parameters.AddWithValue("@d1", txtCustomerID.Text);
226 cmd.Parameters.AddWithValue("@d2", txtCustomerName.Text);
227 cmd.Parameters.AddWithValue("@d3", txtAddress.Text);
228 cmd.Parameters.AddWithValue("@d4", txtCity.Text);
229 cmd.Parameters.AddWithValue("@d5", txtContactNo.Text);
230 cmd.Parameters.AddWithValue("@d6", txtContactNo1.Text);
231 cmd.Parameters.AddWithValue("@d7", txtEmail.Text);
232 cmd.Parameters.AddWithValue("@d8", txtNotes.Text);
233 cmd.ExecuteReader();
234 MessageBox.Show("Successfully updated", "Customer Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
235 btnUpdate.Enabled = false;
236 if (con.State == ConnectionState.Open)
237 {
238 con.Close();
239 }
240
241 con.Close();
242
243 }
244 catch (Exception ex)
245 {
246 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
247 }
248 }
249
250
251
252
253 private void txtContactNo_KeyPress(object sender, KeyPressEventArgs e)
254 {
255 if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
256 {
257 e.Handled = false;
258 }
259 else
260 {
261 e.Handled = true;
262 }
263 }
264
265 private void txtContactNo1_KeyPress(object sender, KeyPressEventArgs e)
266 {
267 if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
268 {
269 e.Handled = false;
270 }
271 else
272 {
273 e.Handled = true;
274 }
275 }
276
277 private void btnGetData_Click(object sender, EventArgs e)
278 {
279 this.Hide();
280 frmCustomersRecord2 frm = new frmCustomersRecord2();
281 frm.Show();
282 frm.GetData();
283 }
284
285 }
286 }